package com.dqhl.business.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.dqhl.business.Base.BaseFragment;
import com.dqhl.business.R;
import com.dqhl.business.adapter.PromotionReleaseAdapter;
import com.dqhl.business.modle.Promotion;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * description:整单促销发布
 * autour: guanluocang
 * date: 2016/11/9 23:51
 */
public class PromotionAllReleaseFragment extends BaseFragment implements View.OnClickListener {

    private ListView lv_allReleasePromotion;
    private PromotionReleaseAdapter mPromotionReleaseAdapter;
    private List<Promotion> mPromotionList;
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    Handler handler = new Handler();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_promotion_all_release, container, false);
        initView(view);
//        initData();
        pullRefresh();
        return view;
    }

    private void pullRefresh() {
        //下拉刷新
        ptrClassicFrameLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh(true);
            }
        }, 150);

        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrClassicFrameLayout.refreshComplete();
                        //获取数据
                        initData();
                    }
                }, 1000);
            }
        });

        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        ptrClassicFrameLayout.loadMoreComplete(true);

                    }
                }, 1000);
            }
        });
    }
    //发布状态 1.未发布 2.发布
    private void initData() {
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.discount_Alllist);
        params.addBodyParameter("seller_id", user.getUser_id());
        params.addBodyParameter("state", "2");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e("GG单品促销未发布" + result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    mPromotionList = JSON.parseArray(data, Promotion.class);
                    mPromotionReleaseAdapter = new PromotionReleaseAdapter(getActivity(), mPromotionList, new PromotionReleaseAdapter.OnReleaserListener() {
                        @Override
                        public void onReleaser(int position) {

                        }
                    });
                    lv_allReleasePromotion.setAdapter(mPromotionReleaseAdapter);
                } else {
                    toast("暂无数据");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });


    }

    private void initView(View view) {
        lv_allReleasePromotion = (ListView) view.findViewById(R.id.lv_allReleasePromotion);
        (ptrClassicFrameLayout) = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_list);
    }

    @Override
    public void onClick(View view) {

    }
}
